//
//  ZYStatusTableViewCell.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/14.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import SnapKit

// 首页字体大小
let HomeFontSize:CGFloat  = 14


// 首页微博的cell
class ZYStatusTableViewCell: UITableViewCell {
    
    var toolBarTop: Constraint?
    
    var statusViewModel: ZYStatusViewModel? {
        
        didSet{
            originalView.statusViewModel = statusViewModel
   
            
            // 卸载约束
            toolBarTop?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.statusViewModel = statusViewModel
                // 显示转发微博View
                retweetView.hidden = false
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                
                // 隐藏
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            toolBar.statusViewModel = statusViewModel
           
        }
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }

    
   // 原创微博视图
    private lazy var originalView:ZYStatusOriginalView = ZYStatusOriginalView()
   // 底部toolbar
    private lazy var toolBar:ZYStatusToolBar = ZYStatusToolBar()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  // 转发视图模型
    private lazy var retweetView:ZYStatusRetweetView = ZYStatusRetweetView()
    
    private func setupUI() {
        // 设置颜色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        // 设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
         
        }
         retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(40)
        }
    
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(self)
        }
        
    }
}
